package com.sec.samsung.android.app.baymax.controll;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.StrictMode;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sec.samsung.android.app.baymax.log.AIOLog;
import com.sec.samsung.android.app.baymax.model.user.Account;
import com.sec.samsung.android.app.baymax.model.user.User;
import com.sec.samsung.android.app.baymax.request.connection.AsyncHttpGet;
import com.sec.samsung.android.app.baymax.request.connection.AsyncHttpPost;
import com.sec.samsung.android.app.baymax.request.connection.AsyncHttpResponseProcess;
import com.sec.samsung.android.app.baymax.request.connection.ParameterFactory;
import com.sec.samsung.android.app.baymax.utils.BaymaxConstant;
import com.sec.samsung.android.app.baymax.utils.BaymaxWebConfig;

public class BaymaxRequestManager {
    // ================================================================
    private Context mContext;

    /* singleton class */
    private static BaymaxRequestManager mInstance;

    /**
     * RequestWrapper constructor
     * 
     * @param context
     */
    public BaymaxRequestManager(Context context) {
        super();
        mContext = context;
    }

    /**
     * get instance of RequestWrapper singleton class
     * 
     * @param context
     * @return instance
     */
    public static synchronized BaymaxRequestManager getInstance(Context context) {
        if (mInstance == null)
            mInstance = new BaymaxRequestManager(context);
        return mInstance;
    }

    //-----------------------POST REQUEST-----------------------------------
    /**
     * post user information
     */
    public void requestToPostUserInfo(Account account, final IRequestListener iRequestListener, Activity activity) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Gson gson = new GsonBuilder().serializeNulls().create();
        User user = new User(account.getId(), account.getName(), account.getPassword(), account.getPassword(), account.getGender(), account.getBirthday());
        String subParamsStr = gson.toJson(user);
        Log.d("KieuThang", "subParamsStr:" + subParamsStr);
        List<NameValuePair> params = ParameterFactory.createSubParams(subParamsStr);
        AsyncHttpPost postRegister = new AsyncHttpPost(activity, new AsyncHttpResponseProcess(activity) {
            @Override
            public void processIfResponseSuccess(String response) {
                AIOLog.d("reponse:" + response);
                JSONObject jsonObject = null;

                try {
                    jsonObject = new JSONObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (jsonObject == null)
                    return;

                String status = null;
                try {
                    status = jsonObject.getString("message");
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                if (BaymaxConstant.SUCCESS.equals(status)) {
                    iRequestListener.onResponse(response);
                } else
                    iRequestListener.onErrorResponse(response);

            }

            @Override
            public void processIfResponseFail() {
                AIOLog.d("response error");
                iRequestListener.onErrorResponse("");
            }
        }, params, true);
        postRegister.execute(BaymaxWebConfig.BAYMAX_USER_PROFILE);
    }

    public void postListBaseSymptoms(ArrayList<String> listSymptomId, final IRequestListener iRequestListener, Activity activity) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Gson gson = new GsonBuilder().serializeNulls().create();
        String subParamsStr = gson.toJson(listSymptomId);
        Log.d("KieuThang", "subParamsStr:" + subParamsStr);
        List<NameValuePair> params = ParameterFactory.createSubParams(subParamsStr);
        AsyncHttpPost postRegister = new AsyncHttpPost(activity, new AsyncHttpResponseProcess(activity) {
            @Override
            public void processIfResponseSuccess(String response) {
                AIOLog.d("reponse:" + response);
                JSONObject jsonObject = null;

                try {
                    jsonObject = new JSONObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (jsonObject == null)
                    return;

                String status = null;
                try {
                    status = jsonObject.getString("message");
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                if (BaymaxConstant.SUCCESS.equals(status)) {
                    iRequestListener.onResponse(response);
                } else
                    iRequestListener.onErrorResponse(response);

            }

            @Override
            public void processIfResponseFail() {
                AIOLog.d("response error");
                iRequestListener.onErrorResponse("");
            }
        }, params, true);
        postRegister.execute(BaymaxWebConfig.SUBMIT_BASE_SYMPTOMS);
    }

    public void postListAnswer(String answerData, final IRequestListener iRequestListener, Activity activity) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Log.d("KieuThang", "subParamsStr:" + answerData);
        List<NameValuePair> params = ParameterFactory.createSubParams(answerData);
        AsyncHttpPost postRegister = new AsyncHttpPost(activity, new AsyncHttpResponseProcess(activity) {
            @Override
            public void processIfResponseSuccess(String response) {
                AIOLog.d("reponse:" + response);
                JSONObject jsonObject = null;

                try {
                    jsonObject = new JSONObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (jsonObject == null)
                    return;

                String status = null;
                try {
                    status = jsonObject.getString("message");
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                if (BaymaxConstant.SUCCESS.equals(status)) {
                    iRequestListener.onResponse(response);
                } else
                    iRequestListener.onErrorResponse(response);

            }

            @Override
            public void processIfResponseFail() {
                AIOLog.d("response error");
                iRequestListener.onErrorResponse("");
            }
        }, params, true);
        postRegister.execute(BaymaxWebConfig.SUBMIT_ANSWER);
    }

    //-----------------------GET REQUEST-----------------------------------
    /**
     * request symptoms suggestion by type
     * 
     * @param iRequestListener
     */
    public void getSymptonSuggestByType(String type, final IRequestListener iRequestListener, Activity activity) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        String url = BaymaxWebConfig.SYMPTONS_SUGGESTION_BY_TYPE;
        url = url.replace("{type}", type);
        Log.d("KieuThang", "url:" + url);
        AsyncHttpGet getLoginRequest = new AsyncHttpGet(mContext, new AsyncHttpResponseProcess(activity) {
            @Override
            public void processIfResponseSuccess(String response) {
                iRequestListener.onResponse(response);
            }

            @Override
            public void processIfResponseFail() {
                AIOLog.d("response error");
                iRequestListener.onErrorResponse("response error");
            }
        }, null, true);

        getLoginRequest.execute(url);

    }

    /**
     * set text by searching symptoms
     * @param text
     * @param iRequestListener
     * @param activity
     */
    public void getTextBySearchingSymptoms(String text, final IRequestListener iRequestListener, Activity activity) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        String url = BaymaxWebConfig.SEARCH_SYMPTOMS;
        url = url.replace("{word}", text);
        Log.d("KieuThang", "url:" + url);
        AsyncHttpGet getLoginRequest = new AsyncHttpGet(mContext, new AsyncHttpResponseProcess(activity) {
            @Override
            public void processIfResponseSuccess(String response) {
                iRequestListener.onResponse(response);
            }

            @Override
            public void processIfResponseFail() {
                AIOLog.d("response error");
                iRequestListener.onErrorResponse("response error");
            }
        }, null, true);

        getLoginRequest.execute(url);

    }
}
